package com.smiier.skin.ui;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.ui.OAlert;
import cn.o.app.ui.UmengWrapper;
import cn.o.app.util.OUtil;
import cn.skinapp.R;

import com.evan.common.constant.Constant;
import com.evan.common.utils.CommonUtility;
import com.lidroid.xutils.BitmapUtils;
import com.smiier.skin.net.AndroidUpdateTask;
import com.smiier.skin.net.AndroidUpdateTask.AndroidUpdateRequest;
import com.smiier.skin.net.AndroidUpdateTask.AndroidUpdateResponse;
import com.smiier.skin.net.AndroidUpdateTask.VersionInfo;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.DBUtil;
import com.umeng.analytics.MobclickAgent;

public class BasicActivity extends UmengWrapper {

	// BEGIN========================兼容外包代码================================

	public Activity activity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		Constant.activitys.put(this.getClass().getSimpleName(), this);
		activity = this;

		if (!CommonUtility.isNull(savedInstanceState)) {
			try {
				GlobalSettings.sUser = JsonUtil.convert(savedInstanceState
						.getString(Constant.IDENTITY_KEY).toString(),
						User.class);
				GlobalSettings.sUid = GlobalSettings.sUser.Uid;
				if(GlobalSettings.sUser.Token != null){
					GlobalSettings.sToken = GlobalSettings.sUser.Token;
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		dbUtil = DBUtil.getInstance(activity);

		mBitmapUtils = new BitmapUtils(this);
	}

	public static void finishAllActivity() {
		CommonUtility.finish();
	}

	protected TextView back, text_nav, text_right;

	protected static DBUtil dbUtil;

	protected void init() {
		back = findViewById(R.id.btn_left, TextView.class);
		text_nav = findViewById(R.id.text_nav, TextView.class);
		text_right = findViewById(R.id.text_right, TextView.class);
	}

	protected void setNavTitle(String title) {
		if (!CommonUtility.isNull(text_nav)) {
			text_nav.setText(title);
		}
	}

	protected void setNavLeftBtn(String title) {
		if (!CommonUtility.isNull(back)) {
			back.setText(title);
		}
	}

	protected void setNavLeftBtnGone() {
		if (!CommonUtility.isNull(back)) {
			back.setVisibility(View.GONE);

		}
	}

	protected void setNavLeftBtnNoDrawable() {
		if (!CommonUtility.isNull(back)) {
			back.setCompoundDrawables(null, null, null, null);
		}
	}

	protected void setNavLeftBtnDrawable(int resid) {
		if (!CommonUtility.isNull(back)) {
			back.setText(null);
			setNavLeftBtnNoDrawable();
			LayoutParams params = (LayoutParams) back.getLayoutParams();
			params.width = CommonUtility.dip2px(activity, 34);
			params.height = CommonUtility.dip2px(activity, 26);
			back.setLayoutParams(params);
			back.setBackgroundResource(resid);
		}
	}

	protected void setNavRightBtn(String title) {
		if (!CommonUtility.isNull(text_right)) {
			text_right.setVisibility(View.VISIBLE);
			text_right.setText(title);
		}
	}

	protected void setNavRightBtnDrawable(int resid) {
		if (!CommonUtility.isNull(text_right)) {
			text_right.setVisibility(View.VISIBLE);
			text_right.setBackgroundResource(resid);
		}
	}

	protected void setNavRightBtnIcon(int resid) {
		if (!CommonUtility.isNull(text_right)) {
			text_right.setVisibility(View.VISIBLE);
			text_right.setCompoundDrawablesWithIntrinsicBounds(null, null, this
					.getResources().getDrawable(resid), null);
		}
	}

	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btn_left) {
			finish();
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (!CommonUtility.isNull(GlobalSettings.sUser)) {
			try {
				outState.putString(Constant.IDENTITY_KEY,
						JsonUtil.convert(GlobalSettings.sUser));
			} catch (Exception e) {

			}
		}
	}

	// END========================兼容外包代码================================

	protected VersionUpdateListener mAppUpdateListener;
	protected String mAppUpdateUrl;

	protected BitmapUtils mBitmapUtils;

	public void checkVersion(boolean runInBackground,
			VersionUpdateListener listener) {
		mAppUpdateListener = listener;
		AndroidUpdateRequest req = new AndroidUpdateRequest();
		AndroidUpdateTask task = new AndroidUpdateTask();
		task.setRequest(req);
		task.addListener(new NetTaskListener<AndroidUpdateTask.AndroidUpdateRequest, AndroidUpdateTask.AndroidUpdateResponse>() {

			@Override
			public void onException(
					INetTask<AndroidUpdateRequest, AndroidUpdateResponse> task,
					Exception e) {
			}

			@Override
			public void onComplete(
					INetTask<AndroidUpdateRequest, AndroidUpdateResponse> task,
					AndroidUpdateResponse response) {
				if (response == null) {
					return;
				}
				
				for (VersionInfo ver : response) {
					if (ver.Platform == null
							|| !ver.Platform
									.equals(GlobalSettings.PLATFORM_ANDROID)) {
						continue;
					}
					String orgin = ver.Ver;
					
					String current = OUtil.getAppVersionName(getContext());
					
					if (ver.Ver.equals(OUtil.getAppVersionName(getContext()))) {
						if (mAppUpdateListener != null) {
							mAppUpdateListener.onNo();
						}
					}/* else if (orgin.length() <= current.length()) {
						
						Log.i("testdoctorupdate", "orgin.length:");
						if (mAppUpdateListener != null) {
							mAppUpdateListener.onNo();
						}
					}*/ else {
						current.replace(".", "");
						if(Long.valueOf(current.replace(".", "")) - Long.valueOf(orgin.replace(".", "")) < 0){
						mAppUpdateUrl = ver.Url;
						
						if (mAppUpdateListener != null) {
							mAppUpdateListener.onYes();
						}
						OAlert alert = new OAlert(getContext());
						alert.setTitle("有新版本发布");
						alert.setOK("立即更新");
						alert.setCancel("稍后再说");
						alert.setListener(new OAlert.OAlertListener() {

							@Override
							public boolean onOK(OAlert alert) {
						
								if (mAppUpdateListener != null) {
									mAppUpdateListener.onUpdate();
								}
								try {
									Intent intent = new Intent();
									intent.setAction("android.intent.action.VIEW");
									intent.setData(Uri.parse(mAppUpdateUrl));
									startActivity(intent);
								} catch (Exception e) {
								}
								return false;
							}

							@Override
							public boolean onCancel(OAlert alert) {
								if (mAppUpdateListener != null) {
									mAppUpdateListener.onUpdateCancel();
								}
								return false;
							}
						});
						alert.show();
						
						}
					}
					return;
				}
			}
		});
		task.setRunInBackground(runInBackground);
		add(task);
	}
	
}
